<html>
<head><meta charset="utf-8"><title>memcpy out-of-bounds · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html">memcpy out-of-bounds</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="148625516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148625516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148625516">(Nov 27 2018 at 09:02)</a>:</h4>
<p>There was an issue somewhere about potential implementations of <code>memcpy</code> using vector instructions wanting to load memory out of bounds but I cannot find it. <span class="user-mention" data-user-id="133214">@briansmith</span>  ?</p>



<a name="148625750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148625750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148625750">(Nov 27 2018 at 09:07)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span>  see <a href="https://github.com/rust-lang/rust/issues/32976#issuecomment-441979177" target="_blank" title="https://github.com/rust-lang/rust/issues/32976#issuecomment-441979177">https://github.com/rust-lang/rust/issues/32976#issuecomment-441979177</a></p>



<a name="148625771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148625771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148625771">(Nov 27 2018 at 09:07)</a>:</h4>
<p>That's a second example about wanting to be able to read and modify memory that's out-of-bounds.</p>



<a name="148629092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148629092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148629092">(Nov 27 2018 at 10:16)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> do you mean <a href="https://github.com/rust-rfcs/unsafe-code-guidelines/issues/2" target="_blank" title="https://github.com/rust-rfcs/unsafe-code-guidelines/issues/2">https://github.com/rust-rfcs/unsafe-code-guidelines/issues/2</a> ?</p>



<a name="148629105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148629105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148629105">(Nov 27 2018 at 10:16)</a>:</h4>
<p>yeah</p>



<a name="148629106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148629106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148629106">(Nov 27 2018 at 10:16)</a>:</h4>
<p>yeah all of that is currently UB until someone gets the LLVM devs to allow it, I think</p>



<a name="148629109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148629109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148629109">(Nov 27 2018 at 10:17)</a>:</h4>
<p>these inbounds assumptions are used pervasively in alias analysis, I would not want to risk violating them</p>



<a name="148629201"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148629201" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148629201">(Nov 27 2018 at 10:19)</a>:</h4>
<p>Check out the Atomic integer issue.</p>



<a name="148629208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148629208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148629208">(Nov 27 2018 at 10:19)</a>:</h4>
<p>Basically, it is being argued that smaller atomic integer types can be emulated on top of larger ones.</p>



<a name="148629217"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148629217" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148629217">(Nov 27 2018 at 10:20)</a>:</h4>
<p>But that requires applying atomic memory operations for a larger type, which access memory out-of-bounds.</p>



<a name="148629272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148629272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148629272">(Nov 27 2018 at 10:20)</a>:</h4>
<p>So maybe the answer here is that no, smaller atomic integer types cannot be emulated on top of larger ones.</p>



<a name="148629648"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148629648" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148629648">(Nov 27 2018 at 10:30)</a>:</h4>
<p>that is, I think, indeed the answer, at least with LLVM as the backend</p>



<a name="148633187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148633187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148633187">(Nov 27 2018 at 11:52)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> even with a different backend, to support this we would have to introduce memory operations that can read / write memory out-of-bounds (whether the reads / writes are atomic, volatile, or something else is irrelevant).</p>



<a name="148633341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148633341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148633341">(Nov 27 2018 at 11:56)</a>:</h4>
<p>sure</p>



<a name="148873153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148873153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148873153">(Nov 30 2018 at 16:46)</a>:</h4>
<p>While at machine level emulating the atomics via CAS loop on a larger memory region is (just fine)™, it has to be done after LLVM (optimisation pipeline) gets it teeth to the code.</p>



<a name="148873160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148873160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148873160">(Nov 30 2018 at 16:46)</a>:</h4>
<p>i.e. implementing this in assembly is fine and so is fine to do so in instruction selection legalisation, I think.</p>



<a name="148885952"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy%20out-of-bounds/near/148885952" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/memcpy.20out-of-bounds.html#148885952">(Nov 30 2018 at 20:13)</a>:</h4>
<p>yeah, maybe you can even do that in LLVM's later lower-level IRs</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>